use cri_api::v1::{ListContainersRequest, ContainerStatsRequest};
use cri_api::v1::runtime_service_client::RuntimeServiceClient;
use tokio::net::UnixStream;
use tonic::transport::{Endpoint, Uri};
use tower::service_fn;
use tokio::main;

#[tokio::main]
async fn main() {
    let path = "/run/containerd/containerd.sock";
    let channel = Endpoint::try_from("http://[::]")
        .unwrap()
        .connect_with_connector(service_fn(move |_: Uri| UnixStream::connect(path)))
        .await
        .expect("Could not create client.");

    let mut client = RuntimeServiceClient::new(channel);

    let containers = client.list_containers(
        ListContainersRequest::default()).await.expect("REASON").into_inner();
    for container in containers.containers {
        let stats = client.container_stats(ContainerStatsRequest {
            container_id: container.id.clone(),
        }).await.expect("REASON").into_inner();

        println!("Container ID: {:?}", container.id);
        println!("Usage: {:?}", stats);

    }
}
